/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.account.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medusa.gruul.account.api.entity.MiniAccountBlanceDetail;
import com.medusa.gruul.account.api.entity.MiniAccountExtends;
import com.medusa.gruul.account.api.model.IntegraChangelDto;
import com.medusa.gruul.account.api.model.MiniAccountExtendsUpdateDto;
import com.medusa.gruul.account.mapper.MiniAccountExtendsMapper;
import com.medusa.gruul.account.model.dto.UpdateUserExtendsInfoDto;
import com.medusa.gruul.account.service.IMiniAccountBlanceDetailService;
import com.medusa.gruul.account.service.IMiniAccountExtendsService;
import com.medusa.gruul.common.core.constant.CommonConstants;
import com.medusa.gruul.common.core.exception.ServiceException;
import com.medusa.gruul.common.core.util.CurUserUtil;
import com.medusa.gruul.common.core.util.SystemCode;
import com.medusa.gruul.common.data.tenant.TenantContextHolder;
import com.medusa.gruul.common.dto.CurUserDto;
import com.medusa.gruul.integral.api.feign.RemoteIntegralService;
import com.medusa.gruul.order.api.model.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户信息扩展表 服务实现类
 * </p>
 *
 * @author whh
 * @since 2019-11-18
 */
@Service
@Slf4j
public class MiniAccountExtendsServiceImpl extends ServiceImpl<MiniAccountExtendsMapper, MiniAccountExtends> implements IMiniAccountExtendsService {

    @Autowired
    private RemoteIntegralService remoteIntegralService;
    @Autowired
    private IMiniAccountBlanceDetailService miniAccountBlanceDetailService;

    @Override
    public MiniAccountExtends findByUserId(String userId) {
        return this.baseMapper.selectOne(new QueryWrapper<MiniAccountExtends>().eq("user_id", userId));
    }

    @Override
    public void integralValueChange(IntegraChangelDto integraChangelDto) {
        TenantContextHolder.setTenantId(integraChangelDto.getTenantId());
        MiniAccountExtends accountExtends = this.findByShopUserId(integraChangelDto.getUserId());
        //增加积分
        if (integraChangelDto.getOption().equals(CommonConstants.NUMBER_ONE)) {
            BigDecimal newIntegraValue = accountExtends.getIntegral().add(integraChangelDto.getValue());
            accountExtends.setIntegral(newIntegraValue);
            this.updateById(accountExtends);
        }
        //减去积分
        if (integraChangelDto.getOption().equals(CommonConstants.NUMBER_TWO)) {
            BigDecimal newIntegraValue = accountExtends.getIntegral().subtract(integraChangelDto.getValue());
            if (newIntegraValue.doubleValue() < 0) {
                log.info("积分不足无法扣减,用户id:{},现有积分:{},需减去积分:{}", accountExtends.getUserId(), accountExtends.getIntegral(), integraChangelDto.getValue());
                return;
            }
            accountExtends.setIntegral(newIntegraValue);
            this.updateById(accountExtends);
        }
    }

    @Override
    public Boolean integralSubtractAffirm(String userId, BigDecimal value, Boolean isDeduct) {
        MiniAccountExtends accountExtends = this.findByShopUserId(userId);
        //jdk 1.8以后 该方法过时
        value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
        if (accountExtends == null) {
            return false;
        }
        BigDecimal newIntegraValue = accountExtends.getIntegral().subtract(value);
        Boolean flag;
        if (newIntegraValue != null && newIntegraValue.doubleValue() > 0) {
            flag = Boolean.TRUE;
        } else {
            flag = Boolean.FALSE;
        }
        if (isDeduct && flag) {
            MiniAccountExtends up = new MiniAccountExtends();
            up.setId(accountExtends.getId());
            up.setIntegral(newIntegraValue);
            flag = Boolean.FALSE;

            if (this.baseMapper.updateById(up) > 0 && remoteIntegralService.integralDeduction(accountExtends.getShopUserId(), value).equals(Boolean.TRUE)) {
                flag = Boolean.TRUE;
            }
        }
        return flag;
    }

    @Override
    public Boolean portionAttributeModify(String userId, MiniAccountExtendsUpdateDto miniAccountExtendsUpdateDto) {
        if (miniAccountExtendsUpdateDto == null) {
            log.info("miniAccountExtendsUpdateDto 对象为null");
            return Boolean.FALSE;
        }
        Integer optionType = miniAccountExtendsUpdateDto.getOptionType();
        boolean isCorrect = optionType < CommonConstants.NUMBER_ZERO || optionType > CommonConstants.NUMBER_THREE;
        if (ObjectUtil.isNull(optionType) || isCorrect) {
            log.info("optionType={},操作类型无效", optionType);
            return Boolean.FALSE;
        }
        MiniAccountExtends accountExtends = findByShopUserId(userId);
        if (accountExtends == null) {
            log.info("userId={},用户扩展数据不存在", userId);
            return Boolean.FALSE;
        }
        MiniAccountExtends updateData = new MiniAccountExtends();
        updateData.setId(accountExtends.getId());
        Boolean flag = Boolean.FALSE;
        switch (optionType) {
            case 1:
                if (ObjectUtil.isEmpty(miniAccountExtendsUpdateDto.getCumsumType()) || ObjectUtil.isEmpty(miniAccountExtendsUpdateDto.getCumsumLimit())
                        || StrUtil.isEmpty(miniAccountExtendsUpdateDto.getBusinessId())) {
                    return Boolean.FALSE;
                }
                flag = consumptionAccumulation(accountExtends, miniAccountExtendsUpdateDto.getCumsumLimit(),
                        miniAccountExtendsUpdateDto.getCumsumType(), miniAccountExtendsUpdateDto.getBusinessId(), miniAccountExtendsUpdateDto.getLastDealTime());
                break;
            //签到数据
            case 3:
                updateData.setClockNum(miniAccountExtendsUpdateDto.getClockNum());
                flag = this.updateById(updateData);
                break;
            default:
                break;
        }
        return flag;
    }

    /**
     * 用户累加消费处理
     *
     * @param accountExtends 用户扩展数据
     * @param cumsumLimit    累加金额
     * @param cumsumType     累加类型  1积分消费  2-订单完成
     * @param businessId     业务id
     * @param lastDealTime   最后交易时间
     */
    private boolean consumptionAccumulation(MiniAccountExtends accountExtends, BigDecimal cumsumLimit, Integer cumsumType, String businessId, LocalDateTime lastDealTime) {
        boolean flag;
        MiniAccountExtends updateData = new MiniAccountExtends();
        updateData.setId(accountExtends.getId());
        updateData.setConsumeTotleMoney(accountExtends.getConsumeTotleMoney().add(cumsumLimit));
        updateData.setConsumeNum(accountExtends.getConsumeNum() + CommonConstants.NUMBER_ONE);
        updateData.setLastDealTime(lastDealTime);
        flag = this.updateById(updateData);
        if (StrUtil.isEmpty(TenantContextHolder.getTenantId())) {
            TenantContextHolder.setTenantId(accountExtends.getTenantId());
        }
        if (!flag) {
            return Boolean.FALSE;
        }

        //记录每次累加金额数据
        MiniAccountBlanceDetail miniAccountBlanceDetail = new MiniAccountBlanceDetail();
        miniAccountBlanceDetail.setShopUserId(accountExtends.getShopUserId());
        miniAccountBlanceDetail.setUserId(accountExtends.getUserId());
        miniAccountBlanceDetail.setShopId(accountExtends.getShopId());
        miniAccountBlanceDetail.setOptionValue(cumsumLimit);
        miniAccountBlanceDetail.setOptionType(CommonConstants.NUMBER_ONE);
        miniAccountBlanceDetail.setOptionSource(cumsumType);
        miniAccountBlanceDetail.setBusinessId(businessId);
        miniAccountBlanceDetailService.save(miniAccountBlanceDetail);
        return flag;
    }

    @Override
    public void updateUserExtendsInfo(UpdateUserExtendsInfoDto updateUserExtendsInfoDto) {
        CurUserDto curUser = CurUserUtil.getHttpCurUser();
        if (curUser == null) {
            throw new ServiceException("用户数据不存在", SystemCode.DATA_NOT_EXIST.getCode());
        }
        MiniAccountExtends miniAccountExtends = this.findByShopUserId(curUser.getUserId());
        if (miniAccountExtends == null) {
            throw new ServiceException("无效用户", SystemCode.DATA_NOT_EXIST.getCode());
        }
        MiniAccountExtends updata = new MiniAccountExtends();
        updata.setId(miniAccountExtends.getId());
        updata.setShopId(updateUserExtendsInfoDto.getShopId());
        updata.setLastChooseLcation(updateUserExtendsInfoDto.getLastChooseLcation());
        this.updateById(updata);
    }

    @Override
    public void orderCompleted(OrderVo orderVo) {
        MiniAccountExtends accountExtends = findByShopUserId(orderVo.getUserId());
        if (ObjectUtil.isNull(accountExtends)) {
            log.debug("用户数据不存在,无法增加消息记录");
            return;
        }
        //用户累加消费处理
        consumptionAccumulation(accountExtends,
                orderVo.getPayAmount(), CommonConstants.NUMBER_TWO, orderVo.getId().toString(), null);
    }

    @Override
    public MiniAccountExtends findByCurrentStatus(String userId) {
        return this.baseMapper.selectByCurrentStatus(userId, CommonConstants.NUMBER_ONE);
    }

    @Override
    public MiniAccountExtends findByShopUserId(String shopUserId) {
        return this.baseMapper.selectOne(new QueryWrapper<MiniAccountExtends>().eq("shop_user_id", shopUserId));
    }

    @Override
    public void modifyLastDealTime(String shopUserId, LocalDateTime lastDealTime) {
        MiniAccountExtends miniAccountExtends = this.findByShopUserId(shopUserId);
        if (miniAccountExtends == null) {
            throw new ServiceException("无效用户");
        }
        MiniAccountExtends updateData = new MiniAccountExtends();
        updateData.setId(miniAccountExtends.getId());
        updateData.setLastDealTime(lastDealTime);
        this.updateById(updateData);
    }

    @Override
    public MiniAccountExtends findByShopIdAndUserId(String shopId, String userId) {
        return this.baseMapper.selectOne(new QueryWrapper<MiniAccountExtends>().eq("user_id", userId).eq("shop_id", shopId));
    }
}
